import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:event_bus/event_bus.dart';
import 'package:flutter/material.dart';

class CustomEvent {
  String msg;
  CustomEvent(this.msg);
}

//建立公共的event bus

EventBus eventBus = new EventBus();
class FirstScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return FirstScreenState();
  }
}

class FirstScreenState extends State<FirstScreen> {

  String msg = "通知";
  StreamSubscription subscription;

  @override
  void initState() {
    subscription = eventBus.on<CustomEvent>().listen((event) {
      setState(() {
        msg += event.msg;
      });
    });
    super.initState();
  }

  @override
  void dispose() {
    subscription.cancel();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Center(child: Column(
      children: [
        Text("$msg"),
        RaisedButton(onPressed: (){
          Navigator.push(context, MaterialPageRoute(builder: (context) => SecondScreen()));
        })
      ],
    ));
  }
}


class SecondScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return SecondScreenState();
  }
}


class SecondScreenState extends State<SecondScreen> {
  @override
  Widget build(BuildContext context) {
    return Center(child: RaisedButton(onPressed: (){
      eventBus.fire(CustomEvent("hello"));
    }, child: Text("fire"),),
    );
  }
}